package com.yjz.middleware.zookeeper;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.RetryNTimes;

import java.util.concurrent.TimeUnit;

/**
 * @author :yuanJinZhong
 * @description: 分布式锁   https://www.cnblogs.com/seaspring/p/5536338.html
 * @date :2019/3/11-15:21
 */
public class CuratorDistrLockTest {
  /**
   * Zookeeper info
   */
  private static final String ZK_ADDRESS = "47.98.237.214:2181";
  private static final String ZK_LOCK_PATH = "/zktest";

  public static void main(String[] args) throws InterruptedException {
    // 1.Connect to zk
    CuratorFramework client = CuratorFrameworkFactory.newClient(
      ZK_ADDRESS,
      new RetryNTimes(10, 5000)
    );
    client.start();
    System.out.println("zk client start successfully!");

    Thread t1 = new Thread(() -> {
      doWithLock(client);
    }, "t1");
    Thread t2 = new Thread(() -> {
      doWithLock(client);
    }, "t2");

    t2.start();
    t1.start();
  }

  private static void doWithLock(CuratorFramework client) {
    InterProcessMutex lock = new InterProcessMutex(client, ZK_LOCK_PATH);
    try {
      if (lock.acquire(10 * 1000, TimeUnit.SECONDS)) {
        System.out.println(Thread.currentThread().getName() + " hold lock");
        Thread.sleep(5000L);
        System.out.println(Thread.currentThread().getName() + " release lock");
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        lock.release();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

  }

}
